package 动态规划;

/**
 * @author: TTDB
 * @create: 2025/9/24
 */
public class canPartition {
    public boolean canPartition(int[] nums) {
        //切分等和数组，找到数组和，如果和为奇数则无法等分
        int sum = 0;
        for (int num : nums) {
            sum += num;
        }
        if (sum % 2 != 0) return false;
        int target = sum / 2;
        //然后再去找是否有子数组的和为target
        boolean[] dp = new boolean[target + 1];
        dp[0] = true;
        for (int i = 0; i < nums.length; i++) {//遍历物品
            for (int j = target; j >= nums[i]; j--) {//遍历背包-一维就从后往前
                dp[j] = dp[j] || dp[j - nums[i]];
            }
        }
        return dp[target];
    }
}
